import smCrypto from 'sm-crypto';

const sm2 = smCrypto.sm2;
const sm3 = smCrypto.sm3;
const sm4 = smCrypto.sm4;
const cipherMode = 1; // 1 - C1C3C2，0 - C1C2C3，默认为1

/**
 * 国密加解密工具类
 */
export default {
  // SM2加密
  doSm2Encrypt(msgString: string, publicKey: string) {
    return sm2.doEncrypt(msgString, publicKey, cipherMode);
  },
  // SM2解密
  doSm2Decrypt(encryptData: string, privateKey: string) {
    return sm2.doDecrypt(encryptData, privateKey, cipherMode);
  },
  // SM2数组加密
  doSm2ArrayEncrypt(msgString: string, publicKey: string) {
    return sm2.doEncrypt(msgString, publicKey, cipherMode);
  },
  // SM2数组解密
  doSm2ArrayDecrypt(encryptData: string, privateKey: string) {
    return sm2.doDecrypt(encryptData, privateKey, cipherMode, { output: 'array' });
  },
  // SM3哈希
  doSm3Hash(msgString: string) {
    return sm3(msgString);
  },
  // SM4 加密
  doSm4Encrypt(msgString: string, key: string) {
    return sm4.encrypt(msgString, key);
  },
  // SM4 CBC加密
  doSm4CbcEncrypt(msgString: string, key: string, iv: string) {
    return sm4.encrypt(msgString, key, { mode: 'cbc', iv: iv });
  },
  // SM4 解密
  doSm4Decrypt(encryptData: string, key: string) {
    return sm4.decrypt(encryptData, key);
  },
  // SM4 CBC解密
  doSm4CbcDecrypt(encryptData: string, key: string, iv: string) {
    return sm4.decrypt(encryptData, key, { mode: 'cbc', iv: iv });
  },
};
